/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: EditRolePresenter.java
 * Date: 02/03/2011
 */

package com.ingenius.sau.client.presenter;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.ingenius.sau.client.AppController;
import com.ingenius.sau.client.Constants;
import com.ingenius.sau.client.event.ListRolesEvent;
import com.ingenius.sau.client.service.UsersServiceAsync;
import com.ingenius.sau.client.view.Display;
import com.ingenius.sau.shared.model.users.Role;

/**
 * Class <strong>EditRolePresenter</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class EditRolePresenter implements EditPresenter {

	public interface EditRoleDisplay extends Display {

		String getName();

		void setName(String name);

		String getDescription();

		void setDescription(String description);

		List<String> getPrivileges();

		void setPrivileges(List<String> privileges);

		void show();

		void hide();
	}

	private final HandlerManager eventBus;
	private final UsersServiceAsync rpcService;
	private final EditRoleDisplay display;
	private Role role;

	public EditRolePresenter(HandlerManager eventBus,
			UsersServiceAsync rpcService, EditRoleDisplay display, String roleId) {

		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = display;
		display.setPresenter(this);

		if (roleId == null) {
			role = new Role();

		} else {
			loadBean(roleId);
		}
	}

	private void loadBean(String roleId) {
		AppController.setStatusMessage("Loading...");
		rpcService.getRole(roleId, new AsyncCallback<Role>() {
			@Override
			public void onSuccess(Role result) {
				if (result == null) {
					onFailure(new Throwable("Database error"));
					return;
				}
				role = result;
				display.setName(role.getName());
				display.setDescription(role.getDescription());
				display.setPrivileges(role.getPrivileges());
				hideStatusMessage();
			}

			@Override
			public void onFailure(Throwable caught) {
				GWT.log(Constants.MESSAGE_FAILED_GET + Role.class.getName(),
						caught);
				role = new Role();
				// TODO i18n
				String failedMessage = "Failed to retrieve role";
				display.showInfo(failedMessage);
				hideStatusMessage();
			}

			void hideStatusMessage() {
				AppController.hideStatusMessage();
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.presenter.EditPresenter#doSaveOrUpdate()
	 */
	@Override
	public void doSaveOrUpdate() {
		role.setName(display.getName());
		role.setDescription(display.getDescription());
		role.setPrivileges(display.getPrivileges());

		// TODO i18n
		final String failedMessage = "Failed to save role";
		final String successMessage = "Role saved sucessfully";

		display.hide();
		AppController.setStatusMessage("Saving...");
		rpcService.saveOrUpdateRole(role, new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					eventBus.fireEvent(new ListRolesEvent());
					display.showInfo(successMessage);
					hideStatusMessage();

				} else {
					onFailure(new Throwable("Database error"));
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				GWT.log(Constants.MESSAGE_FAILED_SAVE + Role.class.getName(),
						caught);
				display.showInfo(failedMessage);
				hideStatusMessage();
			}

			void hideStatusMessage() {
				AppController.hideStatusMessage();
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.presenter.Presenter#go(com.google.gwt.user.client
	 * .ui.HasWidgets)
	 */
	@Override
	public void go(HasWidgets container) {
		display.show();
	}

}
